package com.dingreading.cloud.admin.service.impl;

import com.dingreading.cloud.admin.dto.StoreGoodsDiscountDto;
import com.dingreading.cloud.admin.entity.StoreGoodsDiscount;
import com.dingreading.cloud.admin.entity.table.StoreGoodsDiscountTableDef;
import com.dingreading.cloud.admin.mapper.StoreGoodsDiscountMapper;
import com.dingreading.cloud.admin.service.StoreGoodsDiscountService;
import com.dingreading.cloud.common.util.PageUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import static com.dingreading.cloud.admin.entity.table.StoresTableDef.stores;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2025-07-24
 */
@Service
public class StoreGoodsDiscountServiceImpl extends BaseServiceImpl<StoreGoodsDiscountMapper, StoreGoodsDiscount, StoreGoodsDiscountTableDef> implements StoreGoodsDiscountService {

    @Override
    protected StoreGoodsDiscountTableDef getTable() {
        return StoreGoodsDiscountTableDef.storeGoodsDiscount;
    }

    @Override
    public Page<StoreGoodsDiscountDto> pageList(PageUtil pageUtil, String keyword, Integer enabled) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (enabled != null)
            condition.and(table.enabled.eq(enabled));
        if (StringUtils.isNotBlank(keyword))
            condition.and(stores.storeName.like(keyword));

        QueryWrapper wrapper = QueryWrapper.create()
                .select(table.allColumns, stores.storeName, stores.subjectType)
                .leftJoin(stores).on(table.storeUid.eq(stores.uid))
                .where(condition)
                .orderBy(table.id.desc());

        return getMapper().paginateAs(Page.of(pageUtil.getPage(), pageUtil.getRows()), wrapper, StoreGoodsDiscountDto.class);
    }

    @Override
    public boolean updateInfo(String storeUid, Double discountRate) {
        return UpdateChain.of(StoreGoodsDiscount.class)
                .set(table.discountRate, discountRate)
                .where(table.storeUid.eq(storeUid))
                .update();

    }

    @Override
    public boolean updateEnabled(String storeUid, Integer enabled) {
        return UpdateChain.of(StoreGoodsDiscount.class)
                .set(table.enabled, enabled)
                .where(table.storeUid.eq(storeUid))
                .update();
    }


    @Override
    public StoreGoodsDiscountDto dtoGetByStoreUid(String storeUid) {
        QueryWrapper wrapper = QueryWrapper.create()
                .select(table.allColumns, stores.storeName, stores.subjectType)
                .leftJoin(stores).on(table.storeUid.eq(stores.uid))
                .where(table.storeUid.eq(storeUid));

        return getMapper().selectOneByQueryAs(wrapper, StoreGoodsDiscountDto.class);
    }

    @Override
    public StoreGoodsDiscount getByStoreUid(String storeUid) {
        return getMapper().selectOneByCondition(table.storeUid.eq(storeUid));
    }

    @Override
    public StoreGoodsDiscount getEnabledDiscount(String storeUid) {
        return getMapper().selectOneByCondition(table.storeUid.eq(storeUid).and(table.enabled.eq(1)));
    }

}
